package com.shiningstars.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

import com.shiningstars.R;
import com.shiningstars.general.Config;
import com.shiningstars.general.Constants;
import com.shiningstars.general.Messages;
import com.shiningstars.general.Utilities;
import com.shiningstars.tools.ExceptionHandler;
import com.shiningstars.valuebeans.Feedback;

public class TeacherFeedback2Activity extends Activity implements Constants, Messages, Config {

	Feedback feedback;

	RadioGroup radioGroupPathshalaTime;
	RadioButton radioPathshalaTimeYes;
	RadioButton radioPathshalaTimeNo;
	EditText editTextMinsDelay;
	RadioGroup radioGroupPrayers;
	RadioButton radioPrayersYes;
	RadioButton radioPrayersNo;
	RadioGroup radioGroupGuruvandan;
	RadioButton radioGuruvandanYes;
	RadioButton radioGuruvandanNo;
	RadioGroup radioGroupAttendance;
	RadioButton radioAttendanceYes;
	RadioButton radioAttendanceNo;
	EditText editTextKidsPresent;
	RadioGroup radioGroupRevision;
	RadioButton radioRevisionYes;
	RadioButton radioRevisionNo;
	TextView textViewRevisionResponse;
	RatingBar ratingBarRevisionResponse;
	EditText editTextPage2Comments;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_teacher_feedback_2);

			Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
			SplashPageActivity.showSplash = false;

			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

			radioGroupPathshalaTime = (RadioGroup) findViewById(R.id.tf2RadioGroup1);
			radioPathshalaTimeYes = (RadioButton) findViewById(R.id.tf2RadioButton1);
			radioPathshalaTimeNo = (RadioButton) findViewById(R.id.tf2RadioButton2);
			editTextMinsDelay = (EditText) findViewById(R.id.tf2EditText1);
			radioGroupPrayers = (RadioGroup) findViewById(R.id.tf2RadioGroup2);
			radioPrayersYes = (RadioButton) findViewById(R.id.tf2RadioButton3);
			radioPrayersNo = (RadioButton) findViewById(R.id.tf2RadioButton4);
			radioGroupGuruvandan = (RadioGroup) findViewById(R.id.tf2RadioGroup3);
			radioGuruvandanYes = (RadioButton) findViewById(R.id.tf2RadioButton5);
			radioGuruvandanNo = (RadioButton) findViewById(R.id.tf2RadioButton6);
			radioGroupAttendance = (RadioGroup) findViewById(R.id.tf2RadioGroup4);
			radioAttendanceYes = (RadioButton) findViewById(R.id.tf2RadioButton7);
			radioAttendanceNo = (RadioButton) findViewById(R.id.tf2RadioButton8);
			editTextKidsPresent = (EditText) findViewById(R.id.tf2EditText2);
			radioGroupRevision = (RadioGroup) findViewById(R.id.tf2RadioGroup5);
			radioRevisionYes = (RadioButton) findViewById(R.id.tf2RadioButton9);
			radioRevisionNo = (RadioButton) findViewById(R.id.tf2RadioButton10);
			textViewRevisionResponse = (TextView) findViewById(R.id.tf2TextView1);
			ratingBarRevisionResponse = (RatingBar) findViewById(R.id.tf2RatingBar1);
			editTextPage2Comments = (EditText) findViewById(R.id.tf2EditText3);

			radioGroupPathshalaTime.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					switch (checkedId) {
					case R.id.tf2RadioButton1:
						TeacherFeedback2Activity.this.editTextMinsDelay.setText(BLANK);
						TeacherFeedback2Activity.this.editTextMinsDelay.setEnabled(false);
						break;
					case R.id.tf2RadioButton2:
						TeacherFeedback2Activity.this.editTextMinsDelay.setEnabled(true);
						break;
					}
				}
			});

			ratingBarRevisionResponse.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
					Utilities.setRatingDescription(textViewRevisionResponse, rating);
				}
			});

			if (null != getIntent()) {
				if (null != getIntent().getExtras()) {
					if (null != getIntent().getExtras().get(FEEDBACK)) {
						feedback = (Feedback) getIntent().getExtras().get(FEEDBACK);

						if (YES.equals(feedback.getQuestion12())) {
							radioPathshalaTimeYes.setChecked(true);
							editTextMinsDelay.setText(BLANK);
						} else if (NO.equals(feedback.getQuestion12())) {
							radioPathshalaTimeNo.setChecked(true);
							editTextMinsDelay.setText(feedback.getQuestion13());
						}

						if (YES.equals(feedback.getQuestion14())) {
							radioPrayersYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion14())) {
							radioPrayersNo.setChecked(true);
						}

						if (YES.equals(feedback.getQuestion15())) {
							radioGuruvandanYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion15())) {
							radioGuruvandanNo.setChecked(true);
						}

						if (YES.equals(feedback.getQuestion16())) {
							radioAttendanceYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion16())) {
							radioAttendanceNo.setChecked(true);
						}

						editTextKidsPresent.setText(feedback.getQuestion17());

						if (YES.equals(feedback.getQuestion18())) {
							radioRevisionYes.setChecked(true);
						} else if (NO.equals(feedback.getQuestion18())) {
							radioRevisionNo.setChecked(true);
						}

						if (!Utilities.isBlankString(feedback.getQuestion19())) {
							float rating = Float.parseFloat(feedback.getQuestion19());
							ratingBarRevisionResponse.setRating(rating);
						}

						editTextPage2Comments.setText(feedback.getQuestion20());
					}
				}
			}
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback2Activity.class.getName(), e);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		try {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.feedback_menu, menu);
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback2Activity.class.getName(), e);
		}

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		try {
			if (validate()) {
				Intent i = null;

				if (BACK.equals(item.getTitle())) {
					i = new Intent(TeacherFeedback2Activity.this, TeacherFeedback1Activity.class);
				} else if (NEXT.equals(item.getTitle())) {
					i = new Intent(TeacherFeedback2Activity.this, TeacherFeedback3Activity.class);
				}

				i.putExtra(FEEDBACK, feedback);
				startActivity(i);
				finish();
			}
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback2Activity.class.getName(), e);
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		try {
			AlertDialog.Builder builder = new AlertDialog.Builder(TeacherFeedback2Activity.this);

			builder.setMessage(FEEDBACK_CANCEL);
			builder.setCancelable(false);
			builder.setPositiveButton(OK, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Intent i = null;

					String currentRole = Utilities.getSharedPreference(getApplicationContext(), CURRENT_ROLE);
					if (ADMIN.equals(currentRole)) {
						i = new Intent(TeacherFeedback2Activity.this, AdminActivity.class);
					} else if (TEACHER.equals(currentRole)) {
						i = new Intent(TeacherFeedback2Activity.this, MyFeedbackActivity.class);
					}

					startActivity(i);
					finish();
				}
			});
			builder.setNegativeButton(CANCEL, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {

				}
			});

			AlertDialog alert = builder.create();
			alert.show();
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), TeacherFeedback2Activity.class.getName(), e);
		}
	}

	private boolean validate() {
		boolean isValid = true;
		String errorMessage = null;

		int radioGroupPathshalaTimeId = radioGroupPathshalaTime.getCheckedRadioButtonId();
		if (isValid && radioGroupPathshalaTimeId == -1) {
			isValid = false;
			errorMessage = INVALID_PATHSHALA_TIME;
			radioGroupPathshalaTime.requestFocus();
		} else {
			if (radioGroupPathshalaTimeId == radioPathshalaTimeYes.getId()) {
				feedback.setQuestion12(radioPathshalaTimeYes.getText().toString());
			} else if (radioGroupPathshalaTimeId == radioPathshalaTimeNo.getId()) {
				feedback.setQuestion12(radioPathshalaTimeNo.getText().toString());
			}
		}

		if (isValid && editTextMinsDelay.isEnabled() && Utilities.isBlankString(editTextMinsDelay.getText().toString())) {
			isValid = false;
			errorMessage = INVALID_MINS_DELAY;
			editTextMinsDelay.requestFocus();
		} else {
			feedback.setQuestion13(editTextMinsDelay.getText().toString());
		}

		int radioGroupPrayersId = radioGroupPrayers.getCheckedRadioButtonId();
		if (isValid && radioGroupPrayersId == -1) {
			isValid = false;
			errorMessage = INVALID_PRAYERS;
			radioGroupPrayers.requestFocus();
		} else {
			if (radioGroupPrayersId == radioPrayersYes.getId()) {
				feedback.setQuestion14(radioPrayersYes.getText().toString());
			} else if (radioGroupPrayersId == radioPrayersNo.getId()) {
				feedback.setQuestion14(radioPrayersNo.getText().toString());
			}
		}

		int radioGroupGuruvandanId = radioGroupGuruvandan.getCheckedRadioButtonId();
		if (isValid && radioGroupGuruvandanId == -1) {
			isValid = false;
			errorMessage = INVALID_GURUVANDAN;
			radioGroupGuruvandan.requestFocus();
		} else {
			if (radioGroupGuruvandanId == radioGuruvandanYes.getId()) {
				feedback.setQuestion15(radioGuruvandanYes.getText().toString());
			} else if (radioGroupGuruvandanId == radioGuruvandanNo.getId()) {
				feedback.setQuestion15(radioGuruvandanNo.getText().toString());
			}
		}

		int radioGroupAttendanceId = radioGroupAttendance.getCheckedRadioButtonId();
		if (isValid && radioGroupAttendanceId == -1) {
			isValid = false;
			errorMessage = INVALID_ATTENDANCE;
			radioGroupAttendance.requestFocus();
		} else {
			if (radioGroupAttendanceId == radioAttendanceYes.getId()) {
				feedback.setQuestion16(radioAttendanceYes.getText().toString());
			} else if (radioGroupAttendanceId == radioAttendanceNo.getId()) {
				feedback.setQuestion16(radioAttendanceNo.getText().toString());
			}
		}

		if (isValid && Utilities.isBlankString(editTextKidsPresent.getText().toString())) {
			isValid = false;
			errorMessage = INVALID_KIDS_PRESENT;
			editTextKidsPresent.requestFocus();
		} else {
			feedback.setQuestion17(editTextKidsPresent.getText().toString());
		}

		int radioGroupRevisionId = radioGroupRevision.getCheckedRadioButtonId();
		if (isValid && radioGroupRevisionId == -1) {
			isValid = false;
			errorMessage = INVALID_REVISION;
			radioGroupRevision.requestFocus();
		} else {
			if (radioGroupRevisionId == radioRevisionYes.getId()) {
				feedback.setQuestion18(radioRevisionYes.getText().toString());
			} else if (radioGroupRevisionId == radioRevisionNo.getId()) {
				feedback.setQuestion18(radioRevisionNo.getText().toString());
			}
		}

		if (isValid && ratingBarRevisionResponse.getRating() == 0) {
			isValid = false;
			errorMessage = INVALID_REVISION_RESPONSE;
			ratingBarRevisionResponse.requestFocus();
		} else {
			feedback.setQuestion19(String.valueOf(ratingBarRevisionResponse.getRating()));
		}

		if (isValid && Utilities.isBlankString(editTextPage2Comments.getText().toString())) {
			isValid = false;
			errorMessage = INVALID_PAGE2_COMMENTS;
			editTextPage2Comments.requestFocus();
		} else {
			feedback.setQuestion20(editTextPage2Comments.getText().toString());
		}

		if (!isValid) {
			AlertDialog.Builder builder = new AlertDialog.Builder(TeacherFeedback2Activity.this);

			builder.setMessage(errorMessage);
			builder.setCancelable(false);
			builder.setTitle(ERROR);
			builder.setPositiveButton(OK, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
				}
			});

			AlertDialog alert = builder.create();
			alert.show();
		}

		return isValid;
	}

	@Override
	protected void onPause() {
		super.onPause();
		SplashPageActivity.showSplash = true;
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (SplashPageActivity.showSplash) {
			LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
			View popupView = layoutInflater.inflate(R.layout.activity_splash_page, null);
			final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT, true);

			findViewById(R.id.tf2EditText1).post(new Runnable() {
				public void run() {
					popupWindow.showAtLocation(findViewById(R.id.tf2EditText1), Gravity.CENTER, 0, 0);
				}
			});

			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					if (null != popupWindow && popupWindow.isShowing()) {
						popupWindow.dismiss();
					}
				}
			}, SPLASH_TIME_OUT);

			SplashPageActivity.showSplash = false;
		}
	}
}